package com.snj.test2507;

import java.util.HashMap;
import java.util.Map;

public class MapProcessing {
    public static Map<String, Double> convertObjectToMap(Object obj) {
        Map<String, Double> resultMap = new HashMap<>();
        if (obj == null) {
            return resultMap;
        }
        String str = obj.toString();
        // 处理空映射的情况
        if ("{}".equals(str)) {
            return resultMap;
        }
        // 去除首尾的大括号
        str = str.substring(1, str.length() - 1);
        // 分割键值对
        String[] keyValuePairs = str.split(", ");
        for (String pair : keyValuePairs) {
            String[] parts = pair.split("=");
            String key = parts[0];
            double value = Double.parseDouble(parts[1]);
            // 保留五位小数
            value = (double) Math.round(value * 100000) / 100000;
            resultMap.put(key, value);
        }
        return resultMap;
    }

    public static void main(String[] args) {
        // 模拟 result 对象
        Map<String, Object> result = new HashMap<>();
        result.put("leftRequire", "{连云港本部=0.520012, 灌南=0.2588981, 灌云=0.125}");

        Object leftRequire = result.get("leftRequire");
        System.out.println("对象：" + leftRequire);

        Map<String, Double> processedMap = convertObjectToMap(leftRequire);
        System.out.println("处理后的 Map: " + processedMap);
    }
}